const express = require('express')
const cors = require('cors')
const printer = require('./printer')
const os = require('os')

const app = express();
const port = 3000;
const hostname = os.hostname()

//跨域
app.use(cors())

//原始数据解析
app.use(express.raw())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(express.text())

//打印机路由
app.use('/printer', printer)

//404处理
app.use((req, res, next) => {
    res.status(404).send('404 Not Found.')
})

//错误处理
app.use((err, req, res, next) => {
    console.log('发生错误：' + err)
    res.status(500).send(err)
})

//用hostname方式访问
//http://odd0458:3000/
app.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
})